#!/usr/bin/env bash
#
set -e

if [ "$1" = jetty.sh ]; then
    if ! command -v bash >/dev/null 2>&1 ; then
        cat >&2 <<- 'EOWARN'
            ********************************************************************
            ERROR: bash not found. Use of jetty.sh requires bash.
            ********************************************************************
EOWARN
        exit 1
    fi
    cat >&2 <<- 'EOWARN'
        ********************************************************************
        WARNING: Use of jetty.sh from this image is deprecated and may
        be removed at some point in the future.
        See the documentation for guidance on extending this image:
        https://github.com/docker-library/docs/tree/master/jetty
        ********************************************************************
EOWARN
fi

if ! command -v -- "$1" >/dev/null 2>&1 ; then
    set -- java -jar "$JETTY_HOME/start.jar" "$@"
fi

if [ -n "$TMPDIR" ] ; then
    case "$JAVA_OPTIONS" in
        *-Djava.io.tmpdir=*) ;;
        *) JAVA_OPTIONS="-Djava.io.tmpdir=$TMPDIR $JAVA_OPTIONS" ;;
    esac
fi

if [ "$1" = "java" -a -n "$JAVA_OPTIONS" ] ; then
    shift
    set -- java $JAVA_OPTIONS "$@"
fi

exec "$@"
